package com.dayu.finecomm.mongo.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.dayu.finecomm.mongo.pojo.Gateway;
import com.dayu.finecomm.mongo.pojo.IotDevice;
import com.dayu.finecomm.mongo.pojo.IotPlat;
import com.dayu.finecomm.mongo.pojo.Packet;
import com.dayu.finecomm.mongo.pojo.Rtu;
import com.dayu.finecomm.mongo.pojo.StEfficiency;
import com.dayu.finecomm.mongo.pojo.StSwitch;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;

@Component
public class MongoDAO implements InitializingBean {

	@Value("${mongodb.enable:false}")
	private boolean mongodbEnable = false;

	@Value("${mongodb.db}")
	String dbName;

	@Value("${mongodb.collection.rtu}")
	String collection_name_rtu;

	@Value("${mongodb.collection.gateway}")
	String collection_name_gateway;

	@Value("${mongodb.collection.nbiot}")
	String collection_name_nbiot;

	@Value("${mongodb.collection.nbdev}")
	String collection_name_nbdev;

	@Value("${mongodb.collection.stswitch}")
	String collection_name_stswitch;

	@Value("${mongodb.collection.packet}")
	String collection_name_packet;

	@Value("${mongodb.collection.histroy}")
	String collection_name_histroy;

	@Value("${mongodb.collection.histroy_efficiency}")
	String collection_name_histroy_efficiency;

	@Resource
	MongoClient client;

	public DBCollection rtu;

	public DBCollection gateway;

	public DBCollection nbiot;

	public DBCollection nbdev;

	public DBCollection stswitch;

	public DBCollection packet;

	public DBCollection histroy;

	public DBCollection histroy_efficiency;

	@SuppressWarnings("deprecation")
	@Override
	public void afterPropertiesSet() throws Exception {

		if (!mongodbEnable) {
			return;
		}

		client.setWriteConcern(WriteConcern.ACKNOWLEDGED);

		rtu = client.getDB(dbName).getCollection(collection_name_rtu);
		rtu.setObjectClass(Rtu.class);

		gateway = client.getDB(dbName).getCollection(collection_name_gateway);
		gateway.setObjectClass(Gateway.class);

		nbiot = client.getDB(dbName).getCollection(collection_name_nbiot);
		nbiot.setObjectClass(IotPlat.class);

		nbdev = client.getDB(dbName).getCollection(collection_name_nbdev);
		nbdev.setObjectClass(IotDevice.class);

		stswitch = client.getDB(dbName).getCollection(collection_name_stswitch);
		stswitch.setObjectClass(StSwitch.class);

		packet = client.getDB(dbName).getCollection(collection_name_packet);
		packet.setObjectClass(Packet.class);

		histroy = client.getDB(dbName).getCollection(collection_name_histroy);
		histroy.setObjectClass(StSwitch.class);

		histroy_efficiency = client.getDB(dbName).getCollection(collection_name_histroy_efficiency);
		histroy_efficiency.setObjectClass(StEfficiency.class);
	}

}
